<template>
  <div>
    <div class="shopcart">
      <div class="content">
        <div class="content-left" @click="toggleList">
          <div class="logo-wrapper">
            <div class="logo" :class="{highlight: totalCount > 0}">
              <span class="icon-shopping_cart" :class="{highlight: totalCount > 0}"></span>
            </div>
            <div class="num" v-show="totalCount > 0">{{totalCount}}</div>
          </div>
          <div class="price">￥{{totalPrice}}元</div>
          <div class="desc">另需配送费￥{{this.deliveryPrice}}元</div>
        </div>
        <div class="content-right">
          <div class="pay" :class="payClass">{{payDesc}}</div>
        </div>
      </div>
      <div class="ball-container">
        <div v-for="ball in balls">
          <!-- 两种动画方式，js钩子与css -->
          <transition name="drop" @before-enter="beforeDrop" @enter="dropping" @after-enter="afterDrop">
            <!-- 外层动画 -->
            <div class="ball" v-show="ball.show">
              <!-- 内层动画 -->
              <div class="inner inner-hook"></div>
            </div>
          </transition>
        </div>
      </div>
      <transition name="fold">
        <div class="shopcart-list" v-show="listShow">
          <div class="list-header">
            <div class="title">
              <span>已选商品</span>
            </div>
            <div class="empty" @click="enpty">清空</div>
          </div>
          <div class="list-content" ref="listContent">
            <ul>
              <li
                v-for="(food, index) in this.selectFoods"
                :key="index"
                class="food"
              >
                <span class="name">{{food.name}}</span>
                <div class="price">
                  <span class="totalPrice"><span class="sign">$</span>{{food.price * food.count}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <CartControl :food="food"></CartControl>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>
    </div>
    <transition name="fade">
      <div class="list-mask" v-show="listShow" @click="hideList"></div>
    </transition>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
import CartControl from './common/CartControl'
import BScroll from 'better-scroll'

export default {
  name: 'shopcart',
  props: {
    deliveryPrice: Number,
    minPrice: Number
  },
  components: {
    CartControl
  },
  computed: {
    ...mapState(['balls', 'selectFoods', 'listShow']),
    ...mapGetters(['totalPrice', 'totalCount']),
    payClass () {
    return this.totalPrice >= this.minPrice ? 'enough' : 'not-enough'
    },
    payDesc () {
      if (this.totalPrice === 0) return `$${this.minPrice}元起送`
      else if (this.totalPrice < this.minPrice) {
        let diff = this.minPrice - this.totalPrice
        return `还差元${diff}起送`
      } else return '去结算'
    }
  },
  methods: {
    ...mapMutations(['SHIFT_BALL', 'EMPTY', 'CHANGE_SHOW']),
    toggleList () {
      this.CHANGE_SHOW()
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(this.$refs.listContent, {
            click: true
          })
        } else {
          this.scroll.refresh()
        }
      })
    },
    hideList () {
      this.CHANGE_SHOW()
    },
    enpty () {
      this.EMPTY()
    },
    // 小球执行步骤:
    // 1. 点击cartcontrol组件中的”加号按钮“，$emit添加触发事件，将event.target作为参数，由goods父组件监听
    // 2. goods组件接收事件和target，执行_drop(target)，调用shopcart子组件的drop(el)方法，传入了target
    // 3. shopcart 组件执行 drop(el) 方法时，获取el在视口中的位置，编程式调用ball元素的过渡hook接口，执行css过渡

    // 过渡 钩子
    // 动画的原理是先把它偏移到一个位置，然后按照一定的轨迹和时间完成动画，动画完成后要把位置重置
    beforeDrop (el) { // el 是Vue 钩子选择作用动画的 DOM 对象
      let count = this.balls.length
      while (count--) {
        let ball = this.balls[count]
        if (ball.show) {
          let rect = ball.el.getBoundingClientRect() //获取小球的相对于视口的位移(小球高度)
          let x = rect.left - 32
          let y = -(window.innerHeight - rect.top - 22) //负数,因为是从左上角往下的的方向
          el.style.display = '' //清空display
          el.style.webkitTransform = `translate3d(0,${y}px,0)`
          el.style.transform = `translate3d(0,${y}px,0)`
          //处理内层动画
          let inner = el.getElementsByClassName('inner-hook')[0] //使用inner-hook类来单纯被js操作
          inner.style.webkitTransform = `translate3d(${x}px,0,0)`
          inner.style.transform = `translate3d(${x}px,0,0)`
        }
      }
    },
    dropping (el, done) {  // Vue enter 的第二个参数必须给，否则会设置成同步
      /* eslint-disable no-unused-vars */
      //触发重绘html,可以保证 dom 位置渲染正确后再执行之后的动画,offsetWidth、offsetTop等方法都可以触发
      let rf = el.offsetHeight
      this.$nextTick(() => {  //让动画效果异步执行,提高性能
        el.style.webkitTransform = 'translate3d(0,0,0)'
        el.style.transform = 'translate3d(0,0,0)'
        //处理内层动画
        let inner = el.getElementsByClassName('inner-hook')[0]
        inner.style.webkitTransform = 'translate3d(0,0,0)'
        inner.style.transform = 'translate3d(0,0,0)'
        // 告知Vue，一个小球动画完成
        el.addEventListener('transitionend', done)  // 当动画结束，会有  CSS3 transitionend 事件派发 Vue为了知道过渡的完成，必须设置相应的事件监听器
      })
    },
    afterDrop (el) {
      this.SHIFT_BALL()
      el.style.display = 'none'
    }
  }
}
</script>

<style lang="stylus" scoped>
  .shopcart
    position fixed
    left 0
    bottom 0
    z-index 50
    width 100%
    height 48px
    .content
      display flex
      background #141d27
      color rgba(255,255,255,0.4)
      .content-left
        flex 1
        .logo-wrapper
          float left
          position relative
          top -10px
          margin 0 12px
          padding 6px
          width 56px
          height 56px
          vertical-align top
          box-sizing border-box
          border-radius 50%
          background #141d27
          .logo
            width 100%
            height 100%
            border-radius 50%
            background #2b343c
            text-align: center
            &.highlight
              background #2d97fc
            .icon-shopping_cart
              line-height 44px
              font-size 24px
              color #80858a
              &.highlight
                color #fff
          .num
            position absolute
            top 0
            right 0
            width 24px
            height 16px
            line-height 16px
            text-align center
            border-radius 16px
            font-size 9px
            font-weight 700
            color #fff
            background rgb(240,20,20)
            box-shadow 0 4px 8px 0 rgba(0,0,0,0.4)
        .price
          float left
          padding-right 12px
          margin-top 12px
          line-height 24px
          box-sizing border-box
          border-right 1px solid rgba(255,255,255,0.1)
          font-size 16px
          font-weight 700
          &.highlight
            color #fff
        .desc
          float left
          margin 12px 0 0 12px
          font-size 10px
          line-height 24px
      .content-right
        flex 0 0 105px
        width 105px
        .pay
          height 48px
          line-height 48px
          text-align center
          font-size 12px
          font-weight 700
          background #2b333b
          &.not-enough
            background: #2b333b
          &.enough
            background: #00b43c
            color: #fff
    .ball-container
      .ball
        position fixed
        left 32px
        bottom 22px
        z-index 200
        .inner
          width 16px
          height 16px
          border-radius 50%
          background rgb(0, 160, 220)
        &.drop-enter-active
          transition all .4s cubic-bezier(.49, -0.29, .75, .41)
          .inner
            transition all .4s linear
    .shopcart-list
      position absolute
      top 0
      left 0
      z-index -1
      width: 100%
      transform: translate3d(0, -100%, 0)
      &.fold-enter-active, &.fold-leave-active
        transition all 0.3s
      &.fold-enter, &.fold-leave-to
        transform: translate3d(0, 0, 0)
      .list-header
        padding 0 18px
        height 40px
        line-height 40px
        background: #f3f5f7
        border-bottom 1px solid rgba(7, 17, 27, 0.1)
        .title
          float left
          font-size 14px
          font-weight 700
          color #555
          span
            border-left 3px solid #2d97fc
            padding-left 5px
        .empty
          float right
          font-size 12px
          color rgb(0, 160, 220)
      .list-content
        padding 0 18px
        max-height 240px
        overflow hidden
        background #ffffff
        .food
          position relative
          padding 12px 0
          box-sizing border-box
          .name
            line-height 24px
            font-size 14px
            color rgb(7, 17, 27)
          .price
            position absolute
            right 90px
            bottom 10px
            line-height 26px
            color #fa3a12
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom 5px
  .list-mask
    position fixed
    top 0
    left 0
    width 100%
    height 100%
    z-index 40
    opacity 1
    backdrop-filter blur(10px)
    background rgba(7, 17, 27, 0.6)
    &.fade-enter-active, &.fade-leave-active
      transition all 0.3s
    &.fade-enter, &.fade-leave-to
      opacity 0
      background rgba(0, 0, 0, 0)
</style>
